home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gnat1792.zip / gnat179b / t-adainc / i-cstrin.adb < prev    next >
Text File  |  1994-05-19  |  7KB  |  271 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                 I N T E R F A C E S . C . S T R I N G S                  --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.1 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. with System; use System;
  26. with System.Storage_Elements; use System.Storage_Elements;
  27.  
  28. package body Interfaces.C.Strings is
  29.  
  30.    -----------------------
  31.    -- Local Subprograms --
  32.    -----------------------
  33.  
  34.    package Char_Access is new Address_To_Access_Conversions (Char);
  35.  
  36.    function Peek (From : Chars_Ptr) return Char;
  37.    pragma Inline (Peek);
  38.    --  Given a Chars_Ptr value, obtain referenced character
  39.  
  40.    procedure Poke (Value : Char; Into : Chars_Ptr);
  41.    pragma Inline (Poke);
  42.    --  Given a Chars_Ptr, modify referenced Character value
  43.  
  44.    function "+" (Left : Chars_Ptr; Right : Integer) return Chars_Ptr;
  45.    pragma Inline ("+");
  46.    --  Address arithmetic on Chars_Ptr value
  47.  
  48.    No_Nul_Found : constant Integer := -1;
  49.    function Position_Of_Nul (Into : Char_Array) return Integer;
  50.    --  Returns position of the first Nul in Into or No_Nul_Found (-1) if none.
  51.  
  52.    function C_Malloc (Size : Positive) return Chars_Ptr;
  53.    pragma Import (C, C_Malloc, "malloc");
  54.  
  55.    procedure C_Free (Address : Chars_Ptr);
  56.    pragma Import (C, C_Free, "free");
  57.  
  58.    ---------
  59.    -- "+" --
  60.    ---------
  61.  
  62.    function "+" (Left : Chars_Ptr; Right : Integer) return Chars_Ptr is
  63.    begin
  64.       return Left + Chars_Ptr (Right);
  65.    end "+";
  66.  
  67.    --------------------
  68.    -- Allocate_Chars --
  69.    --------------------
  70.   
  71.    function Allocate_Chars (Chars : in Char_Array) return Chars_Ptr is
  72.       Index : Integer;
  73.  
  74.    begin
  75.       Index := Position_Of_Nul (Into => Chars);
  76.  
  77.       if Index = No_Nul_Found then
  78.          Index := Chars'Last;
  79.       else
  80.          Index := Index - 1;   --  Index may become -1; It's OK.
  81.       end if;
  82.  
  83.       --  Returned value is length of signficant part + 1 for the nul character
  84.  
  85.       return C_Malloc ((Index - Chars'First + 1) + 1);
  86.    end Allocate_Chars;
  87.  
  88.    ---------------------
  89.    -- Allocate_String --
  90.    ---------------------
  91.  
  92.    function Allocate_String (Str : in String) return Chars_Ptr is
  93.    begin
  94.       return Allocate_Chars (To_C (Str));
  95.    end Allocate_String;
  96.  
  97.    ----------
  98.    -- Free --
  99.    ----------
  100.  
  101.    procedure Free (Item : in out Chars_Ptr) is
  102.    begin
  103.       if Item = Null_Ptr then
  104.          return;
  105.       end if;
  106.  
  107.       C_Free (Item);
  108.       Item := Null_Ptr;
  109.    end Free;
  110.  
  111.    ----------
  112.    -- Peek --
  113.    ----------
  114.  
  115.    function Peek (From : Chars_Ptr) return Char is
  116.       use Char_Access;
  117.    begin
  118.       return To_Pointer (Address (To_Address (From))).all;
  119.    end Peek;
  120.  
  121.    ----------
  122.    -- Poke --
  123.    ----------
  124.  
  125.    procedure Poke (Value : Char; Into : Chars_Ptr) is
  126.       use Char_Access;
  127.    begin
  128.       To_Pointer (Address (To_Address (Into))).all := Value;
  129.    end Poke;
  130.  
  131.    ---------------------
  132.    -- Position_Of_Nul --
  133.    ---------------------
  134.  
  135.    function Position_Of_Nul (Into : Char_Array) return Integer is
  136.    begin
  137.       for J in Into'range loop
  138.          if Into (J) = Nul then
  139.             return J;
  140.          end if;
  141.       end loop;
  142.  
  143.       return No_Nul_Found;
  144.    end Position_Of_Nul;
  145.  
  146.    ------------
  147.    -- Strlen --
  148.    ------------
  149.   
  150.    function Strlen (Item : in Chars_Ptr) return Natural is
  151.       Item_Index : Natural := 0;
  152.  
  153.    begin
  154.       loop
  155.          if Peek (Item + Item_Index) = Nul then
  156.             return Item_Index;
  157.          end if;
  158.  
  159.          Item_Index := Item_Index + 1;
  160.       end loop;
  161.    end Strlen;
  162.  
  163.    ------------------
  164.    -- To_Chars_Ptr --
  165.    ------------------
  166.  
  167.    function To_Chars_Ptr
  168.      (Item       : Char_Array_Ptr;
  169.       Null_Check : in Boolean := False)
  170.       return       Chars_Ptr
  171.    is
  172.    begin
  173.       if Item = null then
  174.          return Null_Ptr;
  175.       elsif Null_Check and then
  176.             Position_Of_Nul (Into => Item.all) = No_Nul_Found
  177.       then
  178.          raise Unterminated;
  179.       else
  180.          return To_Integer (Item (Item'First)'Address);
  181.       end if;
  182.    end To_Chars_Ptr;
  183.  
  184.    ------------
  185.    -- Update --
  186.    ------------
  187.   
  188.    procedure Update
  189.      (Item   : in Chars_Ptr;
  190.       Offset : in Natural;
  191.       Chars  : in Char_Array;
  192.       Check  : Boolean := True)
  193.    is
  194.       Index : Chars_Ptr := Item + Offset;
  195.  
  196.    begin
  197.       if Check and then Offset + Chars'Length  > Strlen (Item) then
  198.          raise Update_Error;
  199.       end if;
  200.  
  201.       for J in Chars'range loop
  202.          Poke (Chars (J), Into => Index);
  203.          Index := Index + 1;
  204.       end loop;
  205.    end Update;
  206.   
  207.    procedure Update
  208.      (Item   : in Chars_Ptr;
  209.       Offset : in Natural;
  210.       Str    : in String;
  211.       Check  : Boolean := True)
  212.    is
  213.    begin
  214.       Update (Item, Offset, To_C (Str), Check);
  215.    end Update;
  216.  
  217.    -----------
  218.    -- Value --
  219.    -----------
  220.  
  221.    function Value (Item : in Chars_Ptr) return Char_Array is
  222.       Result : Char_Array (0 .. Strlen (Item));
  223.  
  224.    begin
  225.       if Item = Null_Ptr then
  226.          raise Null_Dereference;
  227.       end if;
  228.  
  229.       --  Note that the following loop will also copy the terminating Nul
  230.  
  231.       for J in Result'range loop
  232.          Result (J) := Peek (Item + J);
  233.       end loop;
  234.  
  235.       return Result;
  236.    end Value;
  237.  
  238.    function Value
  239.      (Item   : in Chars_Ptr;
  240.       Length : in Natural)
  241.       return   Char_Array
  242.    is
  243.       Result : Char_Array (0 .. Length - 1);
  244.  
  245.    begin
  246.       if Item = Null_Ptr then
  247.          raise Null_Dereference;
  248.       end if;
  249.  
  250.       for J in Result'range loop
  251.          Result (J) := Peek (Item + J);
  252.          if Result (J) = Nul then
  253.             return Result (0 .. J);
  254.          end if;
  255.       end loop;
  256.  
  257.       return Result;
  258.    end Value;
  259.  
  260.    function Value (Item : in Chars_Ptr) return String is
  261.    begin
  262.       return To_Ada (Value (Item));
  263.    end Value;
  264.  
  265.    function Value (Item : in Chars_Ptr; Length : in Natural) return String is
  266.    begin
  267.       return To_Ada (Value (Item, Length));
  268.    end Value;
  269.  
  270. end Interfaces.C.Strings;
  271.